package com.example.onlinemusic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.onlinemusic.common.R;
import com.example.onlinemusic.model.domain.Consumer;
import com.example.onlinemusic.model.domain.Token;
import com.example.onlinemusic.model.request.ConsumerRequest;
import com.example.onlinemusic.security.utils.JwtUtils;
import com.example.onlinemusic.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 用户管理控制类
 */
@RestController
public class ConsumerController {

    @Autowired
    ConsumerService consumerService;

    /**
     * 获取所有客户信息
     *
     * @param curPage  当前页
     * @param pageSize 每页数量
     * @return
     */
    @GetMapping("/user")
    public R allUser(@RequestParam(defaultValue = "1") int curPage, @RequestParam(defaultValue = "1") int pageSize) {
        // 分页查询数据
        IPage<Consumer> page = new Page<Consumer>();
        page.setCurrent(curPage);
        page.setSize(pageSize);
        return consumerService.allUser(page);
    }

    /**
     * 根据id删除用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/user/delete")
    public R deleteUser(@RequestParam int id) {
        return consumerService.deleteUser(id);
    }

    /**
     * 删除多个用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/user/deleteIds")
    public R deleteUsers(@RequestParam String[] id) {
        return consumerService.deleteByIds(id);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    // http://localhost:8888/user/detail?id=1
    @GetMapping("/user/detail")
    public R userDetail(@RequestParam int id) {
        return consumerService.userDetail(id);
    }


    /**
     * 首页模块查询用户数据
     *
     * @return
     */
    @GetMapping("/users")
    public R allUser() {
        return consumerService.allUser();
    }

    /**
     * 用户登录
     *
     * @param consumerRequest
     * @param session
     * @return
     */
    @PostMapping("/user/login/status")
    public R loginStatus(@RequestBody ConsumerRequest consumerRequest, HttpSession session) {
        // 调用服务层的校验密码业务
//        R result = consumerService.loginStatus(consumerRequest, session);
//        if (result.getSuccess()) {
//            String tokenStr = JwtUtils.generateToken(consumerRequest.getUsername());
//            Result token = new Result();
//            token.setAuthorization(tokenStr);
//            token.setObj(result.getData());
//            result.setData(token);
//        }
//        return result;
        R result = consumerService.loginStatus(consumerRequest, session);
//        Map<String,Object> map = new HashMap<>();
//        map.put("res",result.getData());
        //登录成功的话，返回中需要包含token信息
        if(result.getSuccess()){
//            String tokenStr = JwtUtils.generateToken(consumerRequest.getUsername());
//            Token token = new Token();
//            token.setAuthorization(tokenStr);
////            map.put("token",token);
//            result.setData(token);
            // 登录成功，生成token并返回给客户端
            String strToken = JwtUtils.generateToken(consumerRequest.getUsername());
            Token token = new Token<>();
            token.setAuthorization(strToken);
            token.setObj(result.getData());
            result.setData(token);
        }

        return result;
    }

    /**
     * 用户注册
     *
     * @param consumerRequest
     * @return
     */
    @PostMapping("/user/add")
    public R addUser(@RequestBody ConsumerRequest consumerRequest) {
        return consumerService.addUser(consumerRequest);
    }

    /**
     * 更新个人资料
     *
     * @param consumerRequest
     * @return
     */
    @PostMapping("/user/update")
    public R updateUser(@RequestBody ConsumerRequest consumerRequest) {
        return consumerService.updateUser(consumerRequest);
    }

}
